package com.example.demo.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.example.demo.po.Album;
import com.example.demo.service.AlbumService;
import com.example.demo.utils.UUIDUtils;

@Controller
@RequestMapping("/admin")
public class AlbumController {
	
	@Autowired
	private AlbumService albumService;
	
	@RequestMapping(value="/adminAlbum",method=RequestMethod.GET)
	public String index(Model model){
		
		List<Album> albumList = albumService.selectAll();
		
		model.addAttribute("albumList",albumList);
		
		return "admin/album/albumList";
			
	}
	
	@RequestMapping(value="/toeditAlbum",method=RequestMethod.GET)
	public String toEditConfig(Model model,String id){
		
		if(id!=null){
			
			Album album=albumService.selectById(id);
			
			model.addAttribute("album", album);
		}
		
		return "admin/album/edit";		
	}
	
	@RequestMapping(value="/editAlbum",method=RequestMethod.POST)
	public String edit(Album album){
		String id = album.getId();
		
		if(id!=null&&id.length()>0) {
			album.setCreatetime(new Date());
			albumService.update(album);
		}
		else {
			id=UUIDUtils.getUUID()+UUIDUtils.getUUID();
			album.setId(id);
			album.setCreatetime(new Date());
			albumService.insert(album);
		}
		
		
		return "redirect:adminAlbum";	
	}
	
	@RequestMapping(value="/deleteAlbum",method=RequestMethod.GET)
	public String delete(String id){
		
		albumService.delete(id);;
		return "redirect:adminAlbum";
	}
}
